----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

EA_Window_WorldMap = {}
EA_Window_WorldMap.ZoneControlData = { }
EA_Window_WorldMap.GlyphData = { }

EA_Window_WorldMap.FADE_TIME = 1.0

EA_Window_WorldMap.currentLevel = 0
EA_Window_WorldMap.currentMap   = 0
EA_Window_WorldMap.autoUpdateFlags   = 0

EA_Window_WorldMap.viewWindows = { }
EA_Window_WorldMap.viewWindows[GameDefs.MapLevel.WORLD_MAP]   = "EA_Window_WorldMapWorldView"
EA_Window_WorldMap.viewWindows[GameDefs.MapLevel.PAIRING_MAP] = "EA_Window_WorldMapPairingView"
EA_Window_WorldMap.viewWindows[GameDefs.MapLevel.ZONE_MAP]    = "EA_Window_WorldMapZoneView"


EA_Window_WorldMap.viewButtons = { }
EA_Window_WorldMap.viewButtons[GameDefs.MapLevel.WORLD_MAP]   = "EA_Window_WorldMapViewModesWorldButton"
EA_Window_WorldMap.viewButtons[GameDefs.MapLevel.PAIRING_MAP] = "EA_Window_WorldMapViewModesPairingButton"
EA_Window_WorldMap.viewButtons[GameDefs.MapLevel.ZONE_MAP]    = "EA_Window_WorldMapViewModesZoneButton"


EA_Window_WorldMap.MAX_TRACKED_QUESTS = 10
EA_Window_WorldMap.currentQuestData = {}

EA_Window_WorldMap.childMapList   = {}
EA_Window_WorldMap.siblingMapList = {}
EA_Window_WorldMap.activateZoom = false


EA_Window_WorldMap.questTrackerWindowCount = 0
EA_Window_WorldMap.activeQuestZone = 0


EA_Window_WorldMap.RealmResourceQuestWindowID = nil
EA_Window_WorldMap.TombKingsQuestWindowID = nil

---------------------------------------------------------------
-- Saved Variables
----------------------------------------------------------------
EA_Window_WorldMap.Settings = {}


----------------------------------------------------------------
-- Local Variables
----------------------------------------------------------------

----------------------------------------------------------------
-- EA_Window_WorldMap Functions
----------------------------------------------------------------

-- OnInitialize Handler
function EA_Window_WorldMap.Initialize()


    WindowRegisterEventHandler( "EA_Window_WorldMap", SystemData.Events.CUSTOM_UI_SCALE_CHANGED,  "EA_Window_WorldMap.OnGlobalUIScaleChange" )
    WindowRegisterEventHandler( "EA_Window_WorldMap", SystemData.Events.PLAYER_ZONE_CHANGED,      "EA_Window_WorldMap.OnPlayerZoneChanged")
    WindowRegisterEventHandler( "EA_Window_WorldMap", SystemData.Events.QUEST_LIST_UPDATED,       "EA_Window_WorldMap.OnQuestListUpdated")
    WindowRegisterEventHandler( "EA_Window_WorldMap", SystemData.Events.QUEST_INFO_UPDATED,       "EA_Window_WorldMap.OnQuestUpdated")
    RRQProgressBar.AddListener( EA_Window_WorldMap.OnRRQsUpdated )

    -- Set the Button States
    for _, buttonName in pairs( EA_Window_WorldMap.viewButtons )
    do
        ButtonSetStayDownFlag( buttonName, true ) 
    end


    EA_Window_WorldMap.InitializeZoneView()
    EA_Window_WorldMap.InitializePairingView()
    EA_Window_WorldMap.InitializeWorldView()
    
    
    EA_Window_WorldMap.OnGlobalUIScaleChange()
    
    EA_Window_WorldMap.OnRRQsUpdated()
end

function EA_Window_WorldMap.OnGlobalUIScaleChange()
    WindowSetScale( "EA_Window_WorldMap", InterfaceCore.GetResolutionScale() )
end

-- OnShutdown Handler
function EA_Window_WorldMap.Shutdown()
    
    EA_Window_WorldMap.ShutdownZoneView()
    EA_Window_WorldMap.ShutdownPairingView()
    EA_Window_WorldMap.ShutdownWorldView()
end

function EA_Window_WorldMap.OnShown()

    -- DEBUG(L"EA_Window_WorldMap.OnShown(), currentLevel = "..EA_Window_WorldMap.currentLevel)
    -- DEBUG(L"  Player Zone = "..GameData.Player.zone )

    -- start on zone-level map the player is in when opened
    EA_Window_WorldMap.SetMap( GameDefs.MapLevel.ZONE_MAP, GameData.Player.zone )
            
    ScreenFlashWindow.SetEnabled( true )
    WindowUtils.OnShown()
end

-- update which information still needs to be auto-updated
-- based on what map elements are visible
-- for instance, if the floating HUD tracker is visible the map shouldn't end RRQ autoupdates just because the map was hidden
function EA_Window_WorldMap.MapAdjustAutoUpdating()

    EA_Window_WorldMap.autoUpdateFlags = 0
    if WindowGetShowing( "EA_Window_RRQTracker" )
    then
        EA_Window_WorldMap.autoUpdateFlags = SystemData.AutoUpdateType.RRQS
    end
    
    if ( WindowGetShowing( "EA_Window_WorldMap" ) and EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.ZONE_MAP )
    then
        EA_Window_WorldMap.autoUpdateFlags = EA_Window_WorldMap.autoUpdateFlags + SystemData.AutoUpdateType.PUBLIC_QUESTS 
    end
    
    if EA_Window_WorldMap.autoUpdateFlags > 0 
    then
        MapBeginAutoUpdating( GameDefs.MapLevel.ZONE_MAP, EA_Window_WorldMap.currentMap, EA_Window_WorldMap.autoUpdateFlags ) 
    else
        -- remove ALL autoupdates
        MapEndAutoUpdating()
    end
    
end

function EA_Window_WorldMap.OnHidden()    
    ScreenFlashWindow.SetEnabled( false )
    WindowUtils.OnHidden()
    EA_Window_WorldMap.MapAdjustAutoUpdating()
end


----------------------------------------------------------------
-- Input Handlers
----------------------------------------------------------------
function EA_Window_WorldMap.Hide()
    BroadcastEvent( SystemData.Events.TOGGLE_WORLD_MAP_WINDOW )
end

function EA_Window_WorldMap.HandleMouseWheel(x, y, delta, flags)
    -- DEBUG(L"EA_Window_WorldMap.HandleMouseWheel("..delta..L", "..x..L", "..y..L")")
    if (delta < 0) then
        EA_Window_WorldMap.ZoomOut()
    elseif (delta > 0) then
        EA_Window_WorldMap.ZoomIn()
    end
end

function EA_Window_WorldMap.OnPlayerZoneChanged()

    -- If the player zones viewing the current zone map, update the map to the new zone.
    
    if( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.ZONE_MAP) 
    then
        EA_Window_WorldMap.SetMap(GameDefs.MapLevel.ZONE_MAP, GameData.Player.zone )
    
    elseif (EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.PAIRING_MAP) 
    then
        EA_Window_WorldMap.UpdatePairingMapCurrentZoneMarker()
    end
    
end

----------------------------------------------------------------
-- Master Navigation
----------------------------------------------------------------
function EA_Window_WorldMap.SetMap( mapLevel, mapIndex )

    --DEBUG(L"EA_Window_WorldMap.SetMap( "..mapLevel..L", "..mapIndex..L" )")
    --DEBUG(L"  currently showing level "..(EA_Window_WorldMap.currentLevel)..L" map "..EA_Window_WorldMap.currentMap)

    -- If nil, do nothing
    if ( (mapLevel == nil) or (mapIndex == nil) ) 
    then
        ERROR( L"EA_Window_WorldMap.SetMap(): Invalid Params" )
        return
    end
    
    -- If we don't have a view for this level, ignore.
    if( EA_Window_WorldMap.viewWindows[ mapLevel ] == nil )
    then
        ERROR( L"EA_Window_WorldMap.SetMap(): Invalid Map Level" )
        return
    end
    
    -- If no change
    if (EA_Window_WorldMap.currentLevel == mapLevel) and
       (EA_Window_WorldMap.currentMap   == mapIndex) 
    then
        MapBeginAutoUpdating( EA_Window_WorldMap.currentLevel, EA_Window_WorldMap.currentMap, EA_Window_WorldMap.autoUpdateFlags )
        
        if( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.ZONE_MAP ) then
            EA_Window_WorldMap.RefreshLockdownTimers()
        end
        
        return
    end
    
    -- Assign the new map
    EA_Window_WorldMap.currentLevel = mapLevel
    EA_Window_WorldMap.currentMap = mapIndex
    EA_Window_WorldMap.autoUpdateFlags = 0
      
    if( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.ZONE_MAP )
    then    
        EA_Window_WorldMap.ShowZone( EA_Window_WorldMap.currentMap )
        EA_Window_WorldMap.autoUpdateFlags = SystemData.AutoUpdateType.PUBLIC_QUESTS             
    elseif( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.PAIRING_MAP )
    then    
        EA_Window_WorldMap.ShowPairing( EA_Window_WorldMap.currentMap )
        EA_Window_WorldMap.MapAdjustAutoUpdating()
    else
        EA_Window_WorldMap.MapAdjustAutoUpdating()
    end   
       
    -- Show only the appropriate view
    for index, windowName in pairs( EA_Window_WorldMap.viewWindows )
    do
        WindowSetShowing( windowName, index == EA_Window_WorldMap.currentLevel )
    end
    
    -- Set the Button States
    for index, buttonName in pairs( EA_Window_WorldMap.viewButtons )
    do
        ButtonSetPressedFlag( buttonName, index == EA_Window_WorldMap.currentLevel ) 
    end

    -- this will show or hide the RRQ Bars/ HUD toggle based on whether we have an RRQ
    -- and whether we are on the correct map level or pairing
    EA_Window_WorldMap.OnRRQsUpdated()
    if WindowGetShowing("EA_Window_WorldMapTombKingsBarContainer") == true
    then
        EA_Window_WorldMap.autoUpdateFlags = EA_Window_WorldMap.autoUpdateFlags + SystemData.AutoUpdateType.RRQS
    end
    -- set up client to request auto-updates on any relevant information that may be displayed on the map screen
    -- note that for anything but a Zone-level map this will be a one-time request rather than repeated updates
    if EA_Window_WorldMap.autoUpdateFlags > 0 
    then
        MapBeginAutoUpdating( EA_Window_WorldMap.currentLevel, EA_Window_WorldMap.currentMap, EA_Window_WorldMap.autoUpdateFlags )   
    end
end


----------------------------------------------------------------
-- View Mode Navigation
-----------------------------------------------------------------

function EA_Window_WorldMap.OnClickViewZoneMapButton()
    if( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.ZONE_MAP )
    then
        return
    end
    
    local playerMaps = MapGetPlayerLocationMaps()
    local newZoom = GameDefs.MapLevel.ZONE_MAP
  
    if (playerMaps[newZoom] ~= nil) then
        EA_Window_WorldMap.SetMap(newZoom, playerMaps[newZoom])
    end
end

function EA_Window_WorldMap.OnMouseOverViewZoneMapButton()
    local text = GetStringFromTable("MapSystem", StringTables.MapSystem.TOOLTIP_ZONE_MAP_BUTTON ) 
    Tooltips.CreateTextOnlyTooltip( SystemData.ActiveWindow.name, text )
    Tooltips.AnchorTooltip(Tooltips.ANCHOR_WINDOW_TOP)
end

function EA_Window_WorldMap.OnClickViewPairingMapButton()
     if( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.PAIRING_MAP )
    then
        return
    end
    
    if( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.ZONE_MAP )
    then
        EA_Window_WorldMap.ZoomOut()    
        return
    end

    if( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.WORLD_MAP )
    then
        EA_Window_WorldMap.ZoomIn()    
        return
    end
end

function EA_Window_WorldMap.OnMouseOverViewPairingMapButton()
    local text = GetStringFromTable("MapSystem", StringTables.MapSystem.TOOLTIP_PAIRING_MAP_BUTTON ) 
    Tooltips.CreateTextOnlyTooltip( SystemData.ActiveWindow.name, text )
    Tooltips.AnchorTooltip(Tooltips.ANCHOR_WINDOW_TOP)
end


function EA_Window_WorldMap.OnClickViewWorldMapButton()
    if( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.WORLD_MAP )
    then
        return
    end

    EA_Window_WorldMap.SetMap(GameDefs.MapLevel.WORLD_MAP, 1)  
end

function EA_Window_WorldMap.OnMouseOverViewWorldMapButton()
    local text = GetStringFromTable("MapSystem", StringTables.MapSystem.TOOLTIP_WORLD_MAP_BUTTON ) 
    Tooltips.CreateTextOnlyTooltip( SystemData.ActiveWindow.name, text )
    Tooltips.AnchorTooltip(Tooltips.ANCHOR_WINDOW_TOP)
end



function EA_Window_WorldMap.ZoomIn()
    -- We're zooming to the child map which we're in.
    local playerMaps = MapGetPlayerLocationMaps()
    local newZoom = EA_Window_WorldMap.currentLevel+1

    if (newZoom <= GameDefs.MapLevel.ZONE_MAP and playerMaps[newZoom] ~= nil) then
        EA_Window_WorldMap.SetMap(newZoom, playerMaps[newZoom])
    end
end

function EA_Window_WorldMap.ZoomOut()
    local parentMap = MapGetParentMap(EA_Window_WorldMap.currentLevel, EA_Window_WorldMap.currentMap)
    
    if (parentMap ~= nil and parentMap.mapLevel ~= nil and parentMap.mapNumber ~= nil) then
        EA_Window_WorldMap.SetMap(parentMap.mapLevel, parentMap.mapNumber)
    end
end

function EA_Window_WorldMap.OnButtonZoom()
    local mouseOverWindow   = WindowGetId(SystemData.MouseOverWindow.name)

    local oneTickAmount = (1 / (GameDefs.MapLevel.NUM_MODES - 1 - 1))
    local newZoomFraction = 1 - (mouseOverWindow * oneTickAmount)
    -- DEBUG(L"EA_Window_WorldMap.ZoomTo() level "..mouseOverWindow)

    EA_Window_WorldMap.activateZoom = true
    SliderBarSetCurrentPosition("EA_Window_WorldMapZoomSliderBar", newZoomFraction)
end

function EA_Window_WorldMap.CreateAppropriateZoneTooltip( zoneId, anchor, clickText )
    -- Find out if it is a normal zone, city, or fort -- we have different types of tooltips for each
    local iconType = nil
    local pairingNum = 0
    for index, pairingData in pairs(EA_Window_WorldMap.pairingMapZones) do
        iconType = pairingData[zoneId]
        if (iconType ~= nil) then
            pairingNum = index
            break
        end
    end
    
    if (iconType ~= nil) then
        -- no rvr campaign data for TK zones (GetCampaignZoneData), but we can still use the RRQ
        if EA_Window_WorldMap.pairingHasZoneControl[pairingNum] == false
        then
            if pairingNum == GameData.ExpansionMapRegion.TOMB_KINGS
            then
                -- zone control is different for Tomb Kings
                local controllingRealm = GameData.Realm.NONE
                local rrqData = RRQProgressBar.GetFirstQuestDataOfType(GameData.RRQDisplayType.ERRQDISPLAY_TOMB_KINGS)
                if rrqData ~= nil and rrqData.realmWithAccess > 0 then
                    controllingRealm = rrqData.realmWithAccess
                end
                Tooltips.CreatePairingMapZoneTooltip( controllingRealm,
                                                      GetZoneName( zoneId ),
                                                      1,
                                                      GetZoneRanksForCurrentRealm( zoneId ),
                                                      SystemData.ActiveWindow.name,
                                                      nil )
            end

            return 
        end
    
        local zoneData = GetCampaignZoneData( zoneId )    
        if (zoneData == nil)
        then
            return
        end
        
        if ((iconType == EA_Window_WorldMap.ICON_ZONE) or (iconType == EA_Window_WorldMap.ICON_ZONE_MINI)) then
            Tooltips.CreatePairingMapZoneTooltip( zoneData.controllingRealm,
                                                  GetZoneName( zoneId ),
                                                  zoneData.tierId,
                                                  GetZoneRanksForCurrentRealm( zoneId ),
                                                  SystemData.ActiveWindow.name,
                                                  anchor,
                                                  clickText )
        elseif ((iconType == EA_Window_WorldMap.ICON_FORT) or (iconType == EA_Window_WorldMap.ICON_FORT_MINI)) then
            Tooltips.CreatePairingMapFortTooltip( zoneData.pairingId,
                                                  zoneData.initialRealm,
                                                  zoneData.controllingRealm,
                                                  GetZoneName( zoneId ),
                                                  GetZoneRanksForCurrentRealm( zoneId ),
                                                  L"", -- extra text
                                                  SystemData.ActiveWindow.name,
                                                  anchor,
                                                  clickText )
        elseif ((iconType == EA_Window_WorldMap.ICON_CITY) or (iconType == EA_Window_WorldMap.ICON_CITY_MINI)) then
            -- zoneId is always the peaceful version. If the city is contested, we must translate it into the contested zone ID.
            local zoneIdForRanks = zoneId
            if (zoneData.controllingRealm ~= zoneData.initialRealm) then
                zoneIdForRanks = MapUtils.GetContestedCityZoneFromPeacefulZone(zoneId)
            end
            
            Tooltips.CreatePairingMapCityTooltip( zoneData.controllingRealm,
                                                  GetZoneName( zoneId ),
                                                  GetZoneRanksForCurrentRealm( zoneIdForRanks ),
                                                  L"", -- extra text
                                                  GameDefs.ZoneCityIds[zoneId],
                                                  SystemData.ActiveWindow.name,
                                                  anchor,
                                                  clickText )
        end
    end
end

function EA_Window_WorldMap.OnTitleMouseOver()
    if (EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.ZONE_MAP) then
        EA_Window_WorldMap.CreateAppropriateZoneTooltip(EA_Window_WorldMap.currentMap, Tooltips.ANCHOR_WINDOW_BOTTOM, L"")
    end
end

--manages setting up the RRQ tracking window elements, but only when there's actually an ongoing RRQ
function EA_Window_WorldMap.OnRRQsUpdated()

    local bShow = false

    -- Tomb Kings tracker on the bottom-right
    rrqData = RRQProgressBar.GetFirstQuestDataOfType(GameData.RRQDisplayType.ERRQDISPLAY_TOMB_KINGS)
    if rrqData ~= nil
    then
        -- lazy window creation, so make sure it exists and create if we need to
        if not DoesWindowExist("EA_Window_WorldMapTombKingsBarContainerStatus")
        then
            -- setup Realm Resource Quest Status Window
            EA_Window_WorldMap.TombKingsQuestWindowID = RRQProgressBar.Create( "EA_Window_WorldMapTombKingsBarContainerStatus", 
                                                                                "EA_Window_WorldMapTombKingsBarContainer",
                                                                                rrqData.displayType)
                                                                                
            --HUD RRQ tracker options, like RvR Campaign tracker
            LabelSetText( "EA_Window_WorldMapTombKingsTrackerToggleName",   GetStringFromTable("WorldControl", StringTables.WorldControl.LABEL_TOGGLE_RRQ)  )
            WindowUtils.AddWindowStateButton( "EA_Window_WorldMapTombKingsTrackerToggleCheckBox", "EA_Window_RRQTracker" )
        end

        if not (RRQProgressBar.GetRRQuestIDfromWindowID( EA_Window_WorldMap.TombKingsQuestWindowID ) == rrqData.rrquestID)
        then
            RRQProgressBar.SetRRQuestID( EA_Window_WorldMap.TombKingsQuestWindowID, rrqData.rrquestID )
        end
        
        if ( EA_Window_WorldMap.currentPairing == GameData.ExpansionMapRegion.TOMB_KINGS )
        then
            EA_Window_WorldMap.UpdatePairingZoneIcons( EA_Window_WorldMap.currentPairing )
        end
        
        -- we only want this visible if there is currently an ongoing RRQ
        bShow = true
    end
    
    -- also, only show on certain views like World Map or Tomb Kings pairing
    bShow = bShow and EA_Window_WorldMap.ShouldShowRRQ()
    WindowSetShowing("EA_Window_WorldMapTombKingsBarContainer", bShow)
    WindowSetShowing("EA_Window_WorldMapTombKingsTrackerToggle", bShow)
    
end

function EA_Window_WorldMap.UpdateRRQTrackerButton()

    local showing = WindowGetShowing( "EA_Window_RRQTracker" )    
    ButtonSetPressedFlag("EA_Window_WorldMapTombKingsTrackerToggleCheckBox", showing )
end

function EA_Window_WorldMap.ToggleHUDRRQTracker()
    local showing = not WindowGetShowing( "EA_Window_RRQTracker" )
    WindowSetShowing( "EA_Window_RRQTracker", showing ) 
    EA_Window_WorldMap.Settings.showHUDRRQTracker = showing
    EA_Window_WorldMap.UpdateRRQTrackerButton()
    
    -- If the floating HUD tracker is visible
    -- the client needs to be auto-updating RRQ data
    EA_Window_WorldMap.MapAdjustAutoUpdating()
end

-- only show the Tomb Kings realm resource quest elements when looking at the World Map, TK Pairing, or TK zone maps
function EA_Window_WorldMap.ShouldShowRRQ()
    if ( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.WORLD_MAP or 
        ( EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.PAIRING_MAP ) or 
        EA_Window_WorldMap.currentLevel == GameDefs.MapLevel.ZONE_MAP and EA_Window_WorldMap.currentMap == 191 )
    then
        return true
    end
    -- also show for any zone whose parent map is the Land of the Dead pairing map. Such as, TK lair instances
    local parentMap = MapGetParentMap(EA_Window_WorldMap.currentLevel, EA_Window_WorldMap.currentMap)
    if parentMap ~= nil and parentMap.mapNumber == GameData.ExpansionMapRegion.TOMB_KINGS
    then
        return true
    end
    return false
end

----------------------------------------------------------------
-- EA_Window_RRQTracker (HUD) Functions
----------------------------------------------------------------

EA_Window_RRQTracker = {}
EA_Window_RRQTracker.layoutRegistered = false
EA_Window_RRQTracker.RealmResourceQuestWindowID = nil

function EA_Window_RRQTracker.Initialize()
    if (EA_Window_WorldMap.Settings.showHUDRRQTracker == nil) then
        -- HUD tracker defaults to ON for level 25+ characters, OFF for others
        EA_Window_WorldMap.Settings.showHUDRRQTracker = (GameData.Player.level >= 25)
    end
    
    WindowRegisterEventHandler( "EA_Window_RRQTracker", SystemData.Events.PLAYER_CAREER_RANK_UPDATED, "EA_Window_RRQTracker.OnRankUpdated") 
    
    RRQProgressBar.AddListener( EA_Window_RRQTracker.OnRRQsUpdated )
    
    EA_Window_RRQTracker.OnRRQsUpdated()
end

function EA_Window_RRQTracker.OnRankUpdated()
    -- When the player levels to rank 25, force the RRQ HUD tracker on. The player can later turn it off if they don't want it.
    if (GameData.Player.level == 25) then
        EA_Window_WorldMap.Settings.showHUDRRQTracker = true
        EA_Window_RRQTracker.OnRRQsUpdated()
    end
end

function EA_Window_RRQTracker.OnShown()
    if( EA_Window_RRQTracker.layoutRegistered == false )
    then
        --DEBUG(L"EA_Window_RRQTracker.OnShown calling LayoutEditor.RegisterWindow")
        LayoutEditor.RegisterWindow( "EA_Window_RRQTracker",
                                    GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_RRQ_STATUS_WINDOW_NAME ),
                                    GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_RRQ_STATUS_WINDOW_DESC ),
                                    false, false,
                                    false, nil )
       EA_Window_RRQTracker.layoutRegistered = true
    end
    
    --in order to save the position of this window it has to be loaded in the .mod file
    --however, if the window was being shown the last time those settings were saved
    --it can show up when there is no RRQ, check to see if we really should show
    if RRQProgressBar.GetFirstQuestDataOfType(GameData.RRQDisplayType.ERRQDISPLAY_TOMB_KINGS) == nil
    then
        WindowSetShowing("EA_Window_RRQTracker", false)
    end
    
    EA_Window_WorldMap.UpdateRRQTrackerButton()
end

function EA_Window_RRQTracker.Hide()
    EA_Window_WorldMap.ToggleHUDRRQTracker()
end

function EA_Window_RRQTracker.OnHidden()
    if( EA_Window_RRQTracker.layoutRegistered == true )
    then
        LayoutEditor.UnregisterWindow( "EA_Window_RRQTracker" )
        EA_Window_RRQTracker.layoutRegistered = false
    end
    
    EA_Window_WorldMap.UpdateRRQTrackerButton()
end

function EA_Window_RRQTracker.OnRRQsUpdated()
    --just tie our tracker to the first RRQ, but could support more later
    local rrqData = RRQProgressBar.GetFirstQuestDataOfType(GameData.RRQDisplayType.ERRQDISPLAY_TOMB_KINGS)
        
    -- if there are RRQs, take the first one and display it
    -- check the Display Type also, some are Tome only and use different bars
    if rrqData ~= nil
    then
        -- lazy window creation, so make sure it exists and create if we need to
        if not DoesWindowExist("EA_Window_RRQTrackerBarContainerStatus")
        then
            
            EA_Window_RRQTracker.RealmResourceQuestWindowID = RRQProgressBar.Create( "EA_Window_RRQTrackerBarContainerStatus", 
                                                                                    "EA_Window_RRQTrackerBarContainer", 
                                                                                    rrqData.displayType) 
            -- note: A change in displayType won't happen until a reloadui, like on zone or relog.
            -- but since RRQs aren't exactly changing more than every week, that should be fine
        end

        if not (RRQProgressBar.GetRRQuestIDfromWindowID( EA_Window_RRQTracker.RealmResourceQuestWindowID ) == rrqData.rrquestID)
        then
            RRQProgressBar.SetRRQuestID( EA_Window_RRQTracker.RealmResourceQuestWindowID, rrqData.rrquestID )
        end

        
        --since we forced it to hide even if they had it open before, show it now that we have data
        --unlike the Campaign HUD tracker, we have to hide this, we probably won't get a message
        -- that just tells us there are no RRQs and then hide on that broadcast. 
        if EA_Window_WorldMap.Settings.showHUDRRQTracker == true and not WindowGetShowing("EA_Window_RRQTracker")
        then
            WindowSetShowing( "EA_Window_RRQTracker", true ) 
            EA_Window_WorldMap.UpdateRRQTrackerButton()
            
            local zone = EA_Window_WorldMap.currentMap
            if zone == nil or zone == 0
            then
                zone = 1 -- may not have our zone yet on initial load, but still need a dummy value to make autoupdates happen
            end
            MapBeginAutoUpdating( GameDefs.MapLevel.ZONE_MAP, zone, SystemData.AutoUpdateType.RRQS ) 
        end
    else
        -- shouldn't be visible if we have nothing useful to show, if we have no quests
        WindowSetShowing("EA_Window_RRQTracker", false)
        EA_Window_WorldMap.UpdateRRQTrackerButton()
    end
    
end
